package actionHandlers;

/*
 * Undo action handler, removes last entry from
 * the shapes array list and pushes it onto the stack
 * repaints
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import palette.CanvasManager;
import palette.CanvasWindow;
import cuShapes.ShapeList;
import java.util.Stack;

/*
 * @author: Daniel Miller
 */

public class UndoHandler implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent arg0) {
		CanvasWindow currWin = ((CanvasWindow)CanvasManager.getInst().getSelectedFrame());
		Stack<ShapeList> undoStack = currWin.getUndoStack();
		if(!undoStack.isEmpty())
		{
			ShapeList item = undoStack.pop();
			try{
				currWin.getRedoStack().push(currWin.getShapeList().getDeepCopy());
			}
			catch(Exception ex){
				System.out.println("Error in deep copy");
			}
			currWin.setShapeList(item);
			currWin.getShapeList().setCurrentShape(null);
			currWin.getShapeList().update();
		}
	}

}
